﻿using MetaProperties.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading;

namespace MetaProperties.Core.Tests
{
    
    
    /// <summary>
    ///This is a test class for MultiThreadedWriteObservableTest and is intended
    ///to contain all MultiThreadedWriteObservableTest Unit Tests
    ///</summary>
    [TestClass()]
    public class MultiThreadedWriteObservableTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Value
        ///</summary>
        public void ValueTestHelper<T>(T defaultValue)
        {
            MultiThreadedWriteObservable<T> target = new MultiThreadedWriteObservable<T>();
            T expected = defaultValue;
            T actual;
            target.Value = expected;
            actual = target.Value;
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void ValueTest()
        {
            ValueTestHelper<GenericParameterHelper>(new GenericParameterHelper(5));
        }

        /// <summary>
        ///A test for ReaderWriterLock
        ///</summary>
        public void ReaderWriterLockTestHelper<T>()
        {
            MultiThreadedWriteObservable<T> target = new MultiThreadedWriteObservable<T>();
            Assert.IsNotNull(target.ReaderWriterLock);
        }

        [TestMethod()]
        public void ReaderWriterLockTest()
        {
            ReaderWriterLockTestHelper<GenericParameterHelper>();
        }

        /// <summary>
        ///A test for Behaviour
        ///</summary>
        public void BehaviourTestHelper<T>()
        {
            MultiThreadedWriteObservable<T> n = new MultiThreadedWriteObservable<T>();
            PrivateObject po = new PrivateObject(n, new PrivateType(typeof(MultiThreadedWriteObservable<T>)));
            MultiThreadedWriteObservable_Accessor<T> target = new MultiThreadedWriteObservable_Accessor<T>(po);

            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsInsideLock, target.Behaviour);

            target.Behaviour = LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible;

            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible, target.Behaviour);
        }

        [TestMethod()]
        [DeploymentItem("MetaProperties.Core.dll")]
        public void BehaviourTest()
        {
            BehaviourTestHelper<GenericParameterHelper>();
        }

        /// <summary>
        ///A test for SetValueRaiseEventsOutsideLock
        ///</summary>
        public void SetValueRaiseEventsOutsideLockTestHelper<T>(T newValue)
        {
            MultiThreadedWriteObservable<T> n = new MultiThreadedWriteObservable<T>();
            PrivateObject po = new PrivateObject(n, new PrivateType(typeof(MultiThreadedWriteObservable<T>)));
            MultiThreadedWriteObservable_Accessor<T> target = new MultiThreadedWriteObservable_Accessor<T>(po);

            bool eventRaised = false;
            n.ValueChanged += (s, e) =>
                {
                    eventRaised = true;
                    Assert.IsFalse(n.ReaderWriterLock.IsWriteLockHeld);
                };

            bool changingEventRaised = false;
            n.PropertyChanging += (s, e) =>
                {
                    changingEventRaised = true;
                    Assert.IsTrue(n.ReaderWriterLock.IsWriteLockHeld);
                };

            target.SetValueRaiseEventsOutsideLock(newValue);

            Assert.IsTrue(eventRaised);
            Assert.IsTrue(changingEventRaised);
        }

        [TestMethod()]
        [DeploymentItem("MetaProperties.Core.dll")]
        public void SetValueRaiseEventsOutsideLockTest()
        {
            SetValueRaiseEventsOutsideLockTestHelper<GenericParameterHelper>(new GenericParameterHelper(34));
        }

        /// <summary>
        ///A test for SetValueRaiseEventsInsideLock
        ///</summary>
        public void SetValueRaiseEventsInsideLockTestHelper<T>(T newValue)
        {
            MultiThreadedWriteObservable<T> n = new MultiThreadedWriteObservable<T>();
            PrivateObject po = new PrivateObject(n, new PrivateType(typeof(MultiThreadedWriteObservable<T>)));
            MultiThreadedWriteObservable_Accessor<T> target = new MultiThreadedWriteObservable_Accessor<T>(po);

            bool eventRaised = false;
            n.ValueChanged += (s, e) =>
            {
                eventRaised = true;
                Assert.IsTrue(n.ReaderWriterLock.IsWriteLockHeld);
            };

            bool changingEventRaised = false;
            n.PropertyChanging += (s, e) =>
            {
                changingEventRaised = true;
                Assert.IsTrue(n.ReaderWriterLock.IsWriteLockHeld);
            };

            target.SetValueRaiseEventsInsideLock(newValue);

            Assert.IsTrue(eventRaised);
            Assert.IsTrue(changingEventRaised); 
        }

        [TestMethod()]
        [DeploymentItem("MetaProperties.Core.dll")]
        public void SetValueRaiseEventsInsideLockTest()
        {
            SetValueRaiseEventsInsideLockTestHelper<GenericParameterHelper>(new GenericParameterHelper(34));
        }

        /// <summary>
        ///A test for GetValue
        ///</summary>
        public void GetValueTestHelper<T>(T value)
        {
            MultiThreadedWriteObservable<T> n = new MultiThreadedWriteObservable<T>(value);
            PrivateObject po = new PrivateObject(n, new PrivateType(typeof(MultiThreadedWriteObservable<T>)));
            MultiThreadedWriteObservable_Accessor<T> target = new MultiThreadedWriteObservable_Accessor<T>(po);

            Assert.AreEqual(value, target.GetValue());
        }

        [TestMethod()]
        [DeploymentItem("MetaProperties.Core.dll")]
        public void GetValueTest()
        {
            GetValueTestHelper<GenericParameterHelper>(new GenericParameterHelper(52));
        }

        /// <summary>
        ///A test for MultiThreadedWriteObservable`1 Constructor
        ///</summary>
        public void MultiThreadedWriteObservableConstructorTest3Helper<T>()
        {
            MultiThreadedWriteObservable<T> target1 = new MultiThreadedWriteObservable<T>(
                LockingObservableBehaviour.RaiseEventsInsideLock);
            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsInsideLock, target1.Behaviour);

            MultiThreadedWriteObservable<T> target2 = new MultiThreadedWriteObservable<T>(
                LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible);
            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible, target2.Behaviour);
        }

        [TestMethod()]
        public void MultiThreadedWriteObservableConstructorTest3()
        {
            MultiThreadedWriteObservableConstructorTest3Helper<GenericParameterHelper>();
        }

        /// <summary>
        ///A test for MultiThreadedWriteObservable`1 Constructor
        ///</summary>
        public void MultiThreadedWriteObservableConstructorTest2Helper<T>()
        {
            MultiThreadedWriteObservable<T> target = new MultiThreadedWriteObservable<T>();
            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsInsideLock, target.Behaviour);
            Assert.AreEqual(default(T), target.Value);
        }

        [TestMethod()]
        public void MultiThreadedWriteObservableConstructorTest2()
        {
            MultiThreadedWriteObservableConstructorTest2Helper<GenericParameterHelper>();
        }

        /// <summary>
        ///A test for MultiThreadedWriteObservable`1 Constructor
        ///</summary>
        public void MultiThreadedWriteObservableConstructorTest1Helper<T>(T value)
        {
            MultiThreadedWriteObservable<T> target = new MultiThreadedWriteObservable<T>(
                LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible, 
                value);

            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsOutsideLockWherePossible, target.Behaviour);
            Assert.AreEqual(value, target.Value);
        }

        [TestMethod()]
        public void MultiThreadedWriteObservableConstructorTest1()
        {
            MultiThreadedWriteObservableConstructorTest1Helper<GenericParameterHelper>(new GenericParameterHelper(37));
        }

        /// <summary>
        ///A test for MultiThreadedWriteObservable`1 Constructor
        ///</summary>
        public void MultiThreadedWriteObservableConstructorTestHelper<T>(T value)
        {
            MultiThreadedWriteObservable<T> target = new MultiThreadedWriteObservable<T>(
                value);

            Assert.AreEqual(LockingObservableBehaviour.RaiseEventsInsideLock, target.Behaviour);
            Assert.AreEqual(value, target.Value);
        }

        [TestMethod()]
        public void MultiThreadedWriteObservableConstructorTest()
        {
            MultiThreadedWriteObservableConstructorTestHelper<GenericParameterHelper>(new GenericParameterHelper(421));
        }
    }
}
